//
//  ModifyProjectTableViewCell.swift
//  XMGG
//
//  Created by iDev on 6/27/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit

class ModifyProjectTableViewCell: UITableViewCell, UITextFieldDelegate {
    let pjImage = UIImageView()
    let pjKeyLable = UILabel()
    let pjValueLable = UITextField()

    var delegate:ModifyProjectCellDelegate?
    var mode:ProjectMode = .view

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
    }
    
    private func setupCell(){
        addSubview(pjImage)
        addSubview(pjKeyLable)
        addSubview(pjValueLable)
    
        pjKeyLable.adjustsFontSizeToFitWidth = true
        pjKeyLable.textColor = UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1)
        pjKeyLable.font = UIFont.init(name: "PingFang SC", size: 15)
    
        pjValueLable.returnKeyType = .done
        pjValueLable.adjustsFontSizeToFitWidth = true
        pjValueLable.clearButtonMode = .whileEditing
        pjValueLable.font = UIFont.init(name: "PingFang SC", size: 14)
        pjValueLable.delegate = self
        
        pjImage.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(23)
            make.width.height.equalTo(23)
            make.centerY.equalToSuperview()
        }
        pjValueLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(200)
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        pjKeyLable.snp.makeConstraints { (make) in
            make.left.equalTo(pjImage.snp.right).offset(5)
            make.right.equalTo(pjValueLable.snp.left)
            make.centerY.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
    }
    
    func setCellTitle(detail:(String, String, String,Bool), index:IndexPath){
        let (img,key,value, writeable) = detail
        var pjImg = UIImage(named: img)
        if pjImg == nil{
            pjImg = UIImage(named: "columu_icondefault")
        }
        pjImage.image = pjImg
        pjKeyLable.text = key
        pjValueLable.text = value
        pjValueLable.placeholder = value
        pjValueLable.tag = index.section*10000+index.row
        if mode == .add{
            pjValueLable.text = ""
        }
        switch mode {
        case .view:
            pjValueLable.isEnabled = false
            pjValueLable.textColor = UIColor.black
        case .edit:
            pjValueLable.isEnabled = writeable
            if writeable {
                pjValueLable.textColor = UIColor(red: 135/255.0, green: 137/255.0, blue: 137/255.0, alpha: 1)
            } else {
                pjValueLable.textColor = UIColor.black
            }
            pjValueLable.clearsOnInsertion = false
        case .add:
            pjValueLable.isEnabled = true
            pjValueLable.textColor = UIColor(red: 135/255.0, green: 137/255.0, blue: 137/255.0, alpha: 1)
            pjValueLable.clearsOnInsertion = true
        }
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        let textY = textField.superview!.frame.origin.y
        if textY > SCREENHEIGHT/3.0 {
            delegate?.keyboardCoverCell(offset: textY-SCREENHEIGHT/3.0)
        }
        firstRespondView = textField
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        firstRespondView?.resignFirstResponder()
        let section = textField.tag/10000
        let row = textField.tag%10000
        delegate?.modifyText(section: section, row: row, data: textField.text ?? "")
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        firstRespondView?.resignFirstResponder()
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        textField.textColor = UIColor.black
        return true
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}

protocol ModifyProjectCellDelegate {
    func keyboardCoverCell(offset: CGFloat)
    func modifyText(section:Int,row:Int,data:String)
}
